package com.electronic.modules.verify.result;

import com.alibaba.fastjson.JSON;
import com.electronic.modules.other.result.ComputerInfo;
import lombok.Data;
import lombok.experimental.SuperBuilder;
import org.springframework.util.StringUtils;

import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author: IFCA
 * @Description: 获取验章结果返回对象
 */
@SuperBuilder
@Data
public class GetOneVerifyResultVO {




    /**
     * 摘要
     */
    private GetOneVerifyAbstractVO verifyAbstract;


    /**
     * 证书信息
     */
    private SignatureCertVO verifyCert;


    /**
     * 印章信息
     */
    private SignatureSealVO verifySeal;

    /**
     * 系统信息
     */
    private ComputerInfo computerInfo;

    public static GetOneVerifyResultVO convert(VerifySignatureResult signatureResult) {
        //验章证书信息
        SignatureCertVO verifyCert = JSON.parseObject(signatureResult.getSignCertAnalysis(), SignatureCertVO.class);

        //验章印章信息
        SignatureSealVO verifySeal = null;
        if (StringUtils.hasText(signatureResult.getSealAnalysis())) {
            verifySeal = JSON.parseObject(signatureResult.getSealAnalysis(), SignatureSealVO.class);
        }

        //签章状态
        boolean signatureStatus = false;
        if (signatureResult.getCertValidity() && signatureResult.getSignatureValidity()) {
            signatureStatus = true;
        }

        //验证结果备注
        Set<String> verifyResultRemark = new HashSet<>();
        if (StringUtils.hasText(signatureResult.getCertValidityRemark())) {
            verifyResultRemark.add(signatureResult.getCertValidityRemark());
        }
        if (StringUtils.hasText(signatureResult.getSignatureValidityRemark())) {
            verifyResultRemark.add(signatureResult.getSignatureValidityRemark());
        }

        //验章摘要
        GetOneVerifyAbstractVO abstractVO = GetOneVerifyAbstractVO.builder()
                .signer(verifyCert.getSigner())
                .signatureStatus(signatureStatus)
                .issuer(verifyCert.getIssuer())
                .validityNotBefore(verifyCert.getValidityNotBefore())
                .validityNotAfter(verifyCert.getValidityNotAfter())
                .signatureTime(signatureResult.getSignatureTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
                .signLocation(signatureResult.getSignLocation())
                .signReason(signatureResult.getSignReason())
                .verifyResultRemark(verifyResultRemark)
                .build();

        ComputerInfo computerInfo1 = ComputerInfo.builder()
                .macAddress(signatureResult.getComputerInfo().getMacAddress())
                .hardDiskSn(signatureResult.getComputerInfo().getHardDiskSn())
                .cpuSn(signatureResult.getComputerInfo().getCpuSn())
                .ipAddress(signatureResult.getComputerInfo().getIpAddress())
                .build();
        //构造验章结果
        GetOneVerifyResultVO resultVO = GetOneVerifyResultVO.builder()
                .verifyCert(verifyCert)
                .verifySeal(verifySeal)
                .verifyAbstract(abstractVO)
                .computerInfo(computerInfo1)
                .build();
        return resultVO;
    }

}
